<?php
class Zend_Controller_Action_Helper_Odt extends Zend_Controller_Action_Helper_Abstract
{
	protected $_reportName = '';


	/**
	 * 送出 odf 檔案
	 *
	 * @param unknown_type $params
	 */
	protected function sendOdt($params='')
	{

		//Openofiice的路徑
		$oo_path = Bootstrap::getPath('data').'/ooo/odt/'.$this->_reportName.'/講師鐘點費';

		//新增一個 zipfile 實例
		$ttt = new Sfs_Filesystem_Zip();

		//加入 xml 檔案到 zip 中，共有五個檔案
		//第一個參數為原始字串，第二個參數為 zip 檔案的目錄和名稱

		if (is_dir($oo_path)) {
			if (($dh = opendir($oo_path)) == true) {
				while (($file = readdir($dh)) !== false) {
					if($file=="." or $file==".." or $file=="content.xml"){
						continue;
					}elseif(is_dir($oo_path."/".$file)){
						$ttt->add_dir("$oo_path/$file/",$file."/");
					}
					else{
						$ttt->add_file(file_get_contents($oo_path."/".$file),$file);
					}
				}
				closedir($dh);
			}
		}
		//讀出 content.xml
		$data = file_get_contents($oo_path."/content.xml");
		// 加入 content.xml 到zip 中
		$ttt->add_file($data,"content.xml");
		$ttt->send_odt($this->_reportName.'.odt');

	}

	// 上傳檔案處理
	// 解壓縮檔案
	protected function _setData($params='')
	{
		// 建立上傳檔案目錄
		//$temp = Sfs_App::getInstance()->getPath('data').'/';
		$temp =Bootstrap::getPath('data').'/ooo/odt/'.$this->_reportName;

		if (!is_dir($temp)){
			Sfs_Filesystem_Dir::makeRecursive($temp);
		}
		if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
			$arr = explode(".",$_FILES['filename']['name']);
			$file_name = end($arr);
			if (strtolower($file_name)=='odt') {
				$file_store_name =  $temp.'/'.Sfs_Filesystem_File::cleanDirname(substr($_FILES['filename']['name'],0,-4)) ;

				$archive = new Sfs_Filesystem_Unzip();
				$archive->extractZip($_FILES['filename']['tmp_name'],$file_store_name); // 解壓縮
				//$odir2 = new Sfs_Filesystem_Dirscan();
				//$odir2->scan($file_store_name,'',true);
				//$odir2->Output();

				//	return  print_r($_FILES,1);
			}
		}
	}

	// 上傳選單
	public function getForm($params='')
	{
		$form = new Zend_Form();
		//$form->setAction('/resource/process')
		if ($this->getRequest()->isPost()) {
			// 取得 view
			$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view;
			$view->message =  $this->_setData($params);
		}
		$form->setMethod('post');
		$form->setAttrib('id', 'uploadodt')
		->setAttrib('enctype', 'multipart/form-data');

		// Instantiating an element and passing to the form object:
		$memo = $form->createElement('text','memo');
		$memo->setLabel('檔案說明');
		$file = $form->createElement('file', 'filename');

		$form->addElement($memo);
		$form->addElement($file);
		$form->addElement('submit','確定');
		return $form;

	}

	// 取得 odt 連結
	private function OdtUrl($style='select')
	{
		$temp =Bootstrap::getPath('data').'/ooo/odt/'.$this->_reportName;

		$arr = Sfs_Filesystem_Dir::getDirectories($temp);
		switch ($style) {
			case 'select':
				$ss = "<select name='selOdtName' id='selOdtName'>";
				foreach($arr as $val) {
					$ss.="<option value='$val'>$val</option>";
				}
				$ss .="</select>";
				break;
			case 'array':
			case 'Array':
				$ss = array();
				foreach($arr as $val){
					$ss[$val]=stat($temp.'/'.$val);
				}
				break;
		}

		return $ss;
	}

	// odt 列表
	private function OdtFileList()
	{
		$temp = Bootstrap::getPath('data').'/ooo/odt/'.$this->_reportName;
		$arr = Sfs_Filesystem_Dir::getDirectories($temp);
		// Use a TabContainer for your form:
		$form = new Zend_Dojo_Form();
		$form->setDecorators(array(
    	'FormElements',
		array('TabContainer', array(
	        'id'          => 'tabContainer',
	        'style'       => 'width: 600px; height: 500px;',
	        'dijitParams' => array(
	            'tabPosition' => 'top'
	            ),
	            )),
	    'DijitForm',
	            ));


	            return $form;
	}

	public function direct($report,$action='getForm',$param='')
	{
		$this->_reportName = $report;
		return $this->{$action}($param);
	}
}
